package com.wondersgroup.aida.tools;

import dev.langchain4j.agent.tool.*;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ToolExecutionResultMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.service.tool.DefaultToolExecutor;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author: 紫麒麟vip
 * @create: 2025/2/15
 * Description:
 */
public class ToolsDemo {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        ChatLanguageModel model = ModelUtil.getOpenAIMOdel();
         //构建工具
         List<ToolSpecification> toolSpecifications = ToolSpecifications.toolSpecificationsFrom(WeatherUtil.class);
         //构建对话消息
         List<ChatMessage> chatMessages = new ArrayList<>();
        UserMessage userMessage = UserMessage.from("济南市今天的天气怎么样？");
        chatMessages.add(userMessage);
        // 第一次与AI大模型交互，获得需要调用的工具。
        Response<AiMessage> aiMessageResponse = model.generate(chatMessages, toolSpecifications);

        AiMessage aiMessage = aiMessageResponse.content();
        System.out.println(aiMessage);
        List<ToolExecutionRequest> toolExecutionRequests = aiMessage.toolExecutionRequests();
        //本地执行工具的方法
        if (aiMessage.hasToolExecutionRequests()) {
            for (ToolExecutionRequest toolExecutionRequest : toolExecutionRequests) {
                String methodName = toolExecutionRequest.name();
                String arguments = toolExecutionRequest.arguments();

                System.out.println("调用的方法：" + methodName);
                System.out.println("调用的参数：" + arguments);
            }
        }
        chatMessages.add(aiMessage);
        WeatherUtil weatherUtil = new WeatherUtil();
        // 将工具调用的方法与聊天一起传给大模型
        toolExecutionRequests.forEach( toolExecutionRequest -> {
            DefaultToolExecutor toolExecutor = new DefaultToolExecutor(weatherUtil, toolExecutionRequest);
            String result = toolExecutor.execute(toolExecutionRequest, UUID.randomUUID().toString());
            System.out.println("自定义工具执行结果：" + result);
            ToolExecutionResultMessage toolExecutionResultMessage = ToolExecutionResultMessage.from(toolExecutionRequest, result);
            chatMessages.add(toolExecutionResultMessage);
        });
        //拿到本地工具执行的最终结果。
        AiMessage finalResponse = model.generate(chatMessages).content();
        System.out.println("最终执行结果：" + finalResponse.text());
    }

    static class WeatherUtil{
        @Tool("获取某一个具体城市的天气")
        public String getWeather(@P("指定的城市") String city) {
            return "今天" + city + "是晴天";
        }
    }
}
